package com.aviccxzx.smarthome.ControlModule;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.aviccxzx.smarthome.Colorplate.ColorSelectDialog;
import com.aviccxzx.smarthome.CommonModule.GosBaseActivity;
import com.aviccxzx.smarthome.R;
import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiDeviceListener;

import java.util.concurrent.ConcurrentHashMap;

public class GosDeviceControlActivity extends GosBaseActivity {

	/** The tv MAC */
	private TextView tvMAC;

	/** The GizWifiDevice device */
	private GizWifiDevice device;

	/** The ActionBar actionBar */
	ActionBar actionBar;

	/** led灯红色值 0-254. */
	private static final String KEY_LIGHT_RED = "LED_R";

	/** led灯绿色值 0-254. */
	private static final String KEY_LIGHT_GREEN = "LED_G";

	/** led灯蓝色值 0-254. */
	private static final String KEY_LIGHT_BLUE = "LED_B";


	/** The sb speed. */
	private SeekBar sbSpeed;

	private View view;
	private ColorSelectDialog colorSelectDialog;
	private int lastColor,red,green,blue;

	/** 设备监听器 */
	private GizWifiDeviceListener deviceListener = new GizWifiDeviceListener() {
		// 接收数据回调
		public void didReceiveData(GizWifiErrorCode result, GizWifiDevice device,
								   ConcurrentHashMap<String, Object> dataMap, int sn) {
			// 已定义的设备数据点，有布尔、数值和枚举型数据
			if (dataMap.get("data") != null) {
				ConcurrentHashMap<String, Object> map = (ConcurrentHashMap<String, Object>) dataMap.get("data");
				// 根据标识名，在回调的map中找到设备上报的值
				if (map.get(KEY_LIGHT_RED) != null) {
					int red = (int) map.get(KEY_LIGHT_RED);
					// 根据设备上报的值更改按钮的图标
//					btnLightSwitch.setSelected(status);
					Toast.makeText(GosDeviceControlActivity.this,red+"--",Toast.LENGTH_LONG).show();
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gos_device_control);
		initDevice();
		initView();
	}

	private void initView() {
		tvMAC = (TextView) findViewById(R.id.tvMAC);
		if (null != device) {

			tvMAC.setText(device.getMacAddress().toString());

		}
		findViewById(R.id.choose_color).setSelected(true);
		findViewById(R.id.choose_color).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (colorSelectDialog == null) {
					colorSelectDialog = new ColorSelectDialog(GosDeviceControlActivity.this);
					colorSelectDialog.setOnColorSelectListener(new ColorSelectDialog.OnColorSelectListener() {
						@Override
						public void onSelectFinish(int color) {
//							lastColor=color;
//							GosDeviceControlActivity.this.view.setBackgroundColor(lastColor);

							red= Color.red(color);
							green=Color.green(color);
							blue=Color.blue(color);

							sendCommand(KEY_LIGHT_RED,red);
//							sendCommand(KEY_LIGHT_GREEN,green);
//							sendCommand(KEY_LIGHT_BLUE,blue);

							Toast.makeText(GosDeviceControlActivity.this,red+"--"+green+"--"+red,Toast.LENGTH_LONG).show();
						}
					});
				}
				colorSelectDialog.setLastColor(lastColor);
				colorSelectDialog.show();
			}
		});
	}

	/**
	 * 发送指令
	 *
	 * @param key
	 *            数据点对应的标识名
	 * @param value
	 *            需要改变的值
	 */
	private void sendCommand(String key, Object value) {
		int sn = 5;
		ConcurrentHashMap<String, Object> hashMap = new ConcurrentHashMap<String, Object>();
		hashMap.put(key, value);
		device.write(hashMap, sn);
		Log.i("Apptest", hashMap.toString());
	}

	private void initDevice() {
		Intent intent = getIntent();
		device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
		device.setListener(deviceListener);
		Log.i("Apptest", device.getDid());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		device.setSubscribe(false);
		device.setListener(null);
	}
}
